package com.wcards.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wcards.config.R;
import com.wcards.dto.AnimalDTO;
import com.wcards.entity.AnimalInfo;
import com.wcards.mapper.AnimalInfoMapper;
import com.wcards.service.AnimalInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Administrator
 */ /*
宠物信息表
 */
@Service
public class AnimalInfoServiceImpl extends ServiceImpl<AnimalInfoMapper,AnimalInfo> implements AnimalInfoService{
    @Autowired
    private AnimalInfoService animalInfoService;
    @Autowired
    private AnimalInfoMapper animalInfoMapper;


    @Override
        public R<Page<AnimalInfo>> getAnimalList (AnimalDTO animalDTO)
        {

            Page<AnimalInfo> pageInfo = new Page<>(animalDTO.getPageNumber(), animalDTO.getPageSize());
            LambdaQueryWrapper<AnimalInfo> wrapper = new LambdaQueryWrapper<AnimalInfo>()
                    .like(animalDTO.getAnimalName() != null, AnimalInfo::getAnimalName, animalDTO.getAnimalName())
                    .eq(animalDTO.getBreed() != null, AnimalInfo::getBreed, animalDTO.getBreed())
                    .eq(animalDTO.getAnimalStatus() != null, AnimalInfo::getAnimalStatus, animalDTO.getAnimalStatus())
                    .eq(animalDTO.getAnimalAge() != null, AnimalInfo::getAnimalAge, animalDTO.getAnimalAge());
            animalInfoService.page(pageInfo, wrapper);
            return R.success(pageInfo);
        }
    @Override
    public R<String> addAnimal(AnimalInfo animalInfo)
    {
        if(animalInfoService.save(animalInfo))
        {
            return R.success("新增成功");
        }
        return R.error("新增失败");
    }
    @Override
    public R<String> deleteAnimal(Integer animalNumbers)
    {
        if(animalInfoService.getById(animalNumbers)!=null)
        {
            if(animalInfoService.removeById(animalNumbers))
            {
                return R.success("删除成功");
            }
            return R.error("删除失败");
        }
        return  R.error("删除失败，动物不存在");
    }
    @Override
    public R<String> updateAnimal(AnimalInfo animalInfo)
    {
        if(animalInfoService.getById(animalInfo.getAnimalNumbers())!=null)
        {
            if(animalInfoService.updateById(animalInfo))
            {
                return R.success("修改成功");
            }
            return R.error("修改失败");
        }
        return R.error("修改失败，动物不存在");
    }

    @Override
    public R<Page<AnimalInfo>> listAdopt(Integer pageSize, Integer pageNumber) {
        Page<AnimalInfo> pageInfo = new Page<>(pageNumber, pageSize);
        QueryWrapper<AnimalInfo> wrapper =new QueryWrapper<>();
        /*为 0 代表可领养*/
        wrapper.eq("animal_status",0);
        animalInfoService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }

    @Override
    public R<Page<AnimalInfo>> listAdopted(Integer pageSize, Integer pageNumber) {
        Page<AnimalInfo> pageInfo = new Page<>(pageNumber, pageSize);
        QueryWrapper<AnimalInfo> wrapper=new QueryWrapper<>();
        /*为 0 代表已经领养*/
        wrapper.eq("animal_status",1);
        animalInfoService.page(pageInfo,wrapper);
        return R.success(pageInfo);
    }

    @Override
    public R<AnimalInfo> getAnimalByNumber(Integer animalNumbers)
    {
        return R.success(animalInfoService.getById(animalNumbers));
    }
}
